<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Evaluates extends Base{
	//评论
	public function evaluateIn()
	{
		if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
		if(!empty(input("param.p_id"))){
			$data['p_id']=input("param.p_id");
		}else{
			return json(['code' => 502, 'msg' => '产品或门票id']);
		}
        if(input("param.type")==1||input("param.type")==2){
            $data['type']=input("param.type");
            if($data['type']==2){
                if(!empty(input("param.star"))){
                    $data['star']=input("param.star");
                }else{
                    return json(['code' => 504, 'msg' => '几星？']);
                }
            }
        }else{
            return json(['code' => 503, 'msg' => '产品还是门票']);
        }
        if(!empty(input("param.message"))){
            $data['message']=input("param.message");
        }else{
            return json(['code' => 505, 'msg' => '评论呢']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $isIn=Db::name('evaluates')->where('user_id',$uid)->where('type',$data['type'])->where('p_id',$data['p_id'])->find();
        if($isIn){
			return json(['code' => 506, 'msg' => '您已评论过此产品或门票']);
        }
		$data['user_id']=$uid;
		$data['state']=2;
		$data['time']=time();
		$data['uptime']=time();
		$res=Db::name('evaluates')->insert($data);
		if ($res) {
            return json(['code' => 200, 'msg' => '成功！']);
		}else{
            return json(['code' => 500, 'msg' => '失败！']);
		}
	}
    // 我的评论
    public function myEvaluates()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        if(input("param.type")==1||input("param.type")==2){
            $data['type']=input("param.type");
        }else{
            return json(['code' => 503, 'msg' => '产品还是门票']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        if($data['type']==1){
            $res=Db::name('evaluates')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.user_id',$uid)->order('a.id desc')->limit(10*($page-1),10)->select();
        }elseif($data['type']==2){
            $res=Db::name('evaluates')->alias('a')->join('scenic_spot b', 'a.p_id = b.id','LEFT')->field('a.*,b.title,b.img')->where('a.user_id',$uid)->order('a.id desc')->limit(10*($page-1),10)->select();
        }
        if ($res) {
            foreach ($res as $key => $value) {
                $res[$key]['time'] = date('Y-m-d H:i:s',$res[$key]['time']);
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
    // 某产品或门票评论
    public function productEvaluates()
    {
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 501, 'msg' => '产品或门票id']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        if(input("param.type")==1||input("param.type")==2){
            $data['type']=input("param.type");
        }else{
            return json(['code' => 503, 'msg' => '产品还是门票']);
        }
        $res=Db::name('evaluates')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->field('a.*,b.headimgurl,b.nickname,b.phone')->where('a.p_id',$p_id)->where('a.state',1)->where('a.type',$data['type'])->order('a.id desc')->limit(10*($page-1),10)->select();
        if ($res) {
            foreach ($res as $key => $value) {
                $res[$key]['phone'] = substr_replace($res[$key]['phone'], '****', 3, 4);
                $res[$key]['time'] = date('Y-m-d H:i:s',$res[$key]['time']);
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
}